﻿@{
    var pushPin = string.Empty;
    if (Model.PinIcon != null || Model.PinText != null)
    {
        var pinIcon = Model.PinIcon == null ? ";" : Model.PinIcon;
        var pinText = Model.PinText == null ? string.Empty : ";" + Model.PinText;
        pushPin = "&pp=" + Model.Latitude + "," + Model.Longitude + ";" + pinIcon + pinText;
    }
    var mapSize = string.Empty;

    if (Model.Width < 0)
    {
        Model.Width = 0;
    }
    if (Model.Height < 0)
    {
        Model.Height = 0;
    }

    if (Model.MapMode == "Static")
    {
        if (Model.Width > 0 && Model.Height > 0)
        {
            mapSize = "mapSize=" + Model.Width.ToString() + "," + Model.Height.ToString();
        }

        var source = "http://dev.virtualearth.net/REST/v1/Imagery/Map/" + Model.Mode + "/" + Model.Latitude + "," + Model.Longitude + "/" + Model.Zoom + "?" + mapSize + pushPin + "&key=" + Model.ApiKey;

    <img alt="Location" src=@source />
    }
    else
    {
        Script.Require("jQuery");
        Script.Include("http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0");
        var mapIndex = (Context.Items["LoadIn.Maps.Bing.Index"] as int?) ?? 0;
        Context.Items["LoadIn.Maps.Bing.Index"] = mapIndex + 1;

        if (Model.Width > 0 && Model.Height > 0)
        {
            mapSize = "width:" + Model.Width + "px; height:" + Model.Height + "px;";
        }
        else
        {
            mapSize = "width:" + 500 + "px; height:" + 400 + "px;";
        }

        var source = "position:relative;" + mapSize;

        using (Script.Foot())
        {
    <script type="text/javascript">
                var map = null;

                var directionsManager;

                map = new Microsoft.Maps.Map(document.getElementById('BingMap'), { credentials: '@Model.ApiKey' });
                map.setView({ zoom: @Model.Zoom, center: new Microsoft.Maps.Location(@Model.Latitude, @Model.Longitude) });
                var offset = new Microsoft.Maps.Point(0, 5);
                var pushpinOptions = {Icon: @Model.PinIcon, text : '@Model.PinText', visible: true, textOffset: offset};
                var pushpin= new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(@Model.Latitude, @Model.Longitude), pushpinOptions);
                map.entities.push(pushpin);

                var geoLocationProvider = new Microsoft.Maps.GeoLocationProvider(map);
                geoLocationProvider.getCurrentPosition({ showAccuracyCircle: false, updateMapView: false });

    </script>
        }

    <div id='BingMap' style="@source">
    </div>
    }
}